package net.diaowen;

import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.io.util.StreamUtil;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.draw.DashedLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.LineSeparator;
import com.itextpdf.layout.element.Paragraph;

import java.io.IOException;
import java.io.InputStream;


public class TestPDF {
    public static void main(String[] args) throws IOException {
        PdfWriter writer = new PdfWriter("/Users/fanjunwei/Downloads/test.pdf");
        PdfDocument pdfDocument = new PdfDocument(writer);

        InputStream inputStream = TestPDF.class.getResourceAsStream("/FangZhengShuSongJianTi-1.ttf");
        byte[] data;
        if (inputStream != null) {
            data = StreamUtil.inputStreamToArray(inputStream);
        } else {
            throw new IOException("字体文件不存在");
        }
        FontProgram fontPro = FontProgramFactory.createFont(data, true);

        PdfFont font = PdfFontFactory.createFont(fontPro);


        Document document = new Document(pdfDocument, PageSize.A4);
        pdfDocument.addFont(font);
        document.setFont(font);
        document.setMargins(20, 20, 20, 20);
        for (int i = 0; i < 100; i++) {
            document.add(new Paragraph("12 你好 Hello World 你好").setMarginLeft(10).setFirstLineIndent(10).setFontColor(new DeviceRgb(13, 102, 239)));
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < 100; j++) {
                sb.append("12 你好 Hello World 你好");
            }
            document.add(new Paragraph(sb.toString()).setMarginLeft(30).setFirstLineIndent(10).setFontColor(new DeviceRgb(229, 226, 230)));
            if (i % 2 == 0) {
                LineSeparator dashedLine = new LineSeparator(new DashedLine(2));
                document.add(dashedLine);
            }
        }
//        document.add(new Image(ImageDataFactory.create("/Users/fanjunwei/Downloads/MicrosoftYaHei-01.ttf")));
        pdfDocument.close();
    }
}
